<template>
  <van-icon :color="value?'hotpink':'#777'" :name="value?'star':'star-o'" @click="onCollect" :load="onLoading" />
</template>

<script>
// 导入接口
import { collectArticleList, cancelCollectArticle } from '@/api/article.js'
export default {
  name: 'collect-article',
  props: {
    value: {
      type: Boolean,
      require: true
    },
    articleId: {
      type: [Object, Number],
      require: true
    }
  },
  data () {
    return {
      onLoading: false
    }
  },
  methods: {
    async onCollect () {
      this.onLoading = true
      try {
        if (this.value) {
          await cancelCollectArticle(this.articleId)
        } else {
          await collectArticleList(this.articleId)
        }
        this.$emit('input', !this.value)
        this.$toast.success(this.value ? '取消收藏' : '收藏成功')
      } catch (err) {
        console.log(err)
        this.$toast('操作失误，请重试')
      }
      this.onLoading = false
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style lang='less' scoped>
.van-icon {
  font-size: 40px;
}
</style>
